//
//  CircleProgressLayer.m
//  Pods
//
//  Created by 张俊博 on 16/8/26.
//
//

#import "CircleProgressLayer.h"
#import "NSString+QTAdd.h"

#import <UIFont+QTAdd.h>

@interface CircleProgressLayer ()

@property (nonatomic, strong) CAShapeLayer  *baseLayer;
@property (nonatomic, strong) CAShapeLayer  *progressLayer;
@property (nonatomic, strong) CATextLayer   *textLayer;

@property (nonatomic, strong) NSNumberFormatter *numberFormatter;

@end

@implementation CircleProgressLayer {
    CGPoint center;
    CGFloat radius;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit{
    
    self.bounds = CGRectMake(0, 0, 37.0, 37.0);
    self.anchorPoint = CGPointMake(.5f, .5f);
    
    center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0);
    radius = MIN(self.bounds.size.width, self.bounds.size.height) / 2.0;
    
    self.baseLayer = [CAShapeLayer layer];
    self.baseLayer.lineCap = kCALineCapRound;
    self.baseLayer.lineJoin = kCALineJoinRound;
    self.baseLayer.lineWidth = 3.0f;
    
    self.baseLayer.fillColor = nil;
    
    [self addSublayer:self.baseLayer];
    
    self.progressLayer = [CAShapeLayer layer];
    self.progressLayer.lineCap = kCALineCapRound;
    self.progressLayer.lineJoin = kCALineJoinRound;
    self.progressLayer.lineWidth = 3.0;
    self.progressLayer.fillColor = nil;
    [self addSublayer:self.progressLayer];
    
    self.textLayer = [CATextLayer layer];
    self.textLayer.alignmentMode = kCAAlignmentCenter;
    self.textLayer.contentsScale = [UIScreen mainScreen].scale;
    self.textLayer.fontSize = 12;
    self.textLayer.anchorPoint = CGPointMake(0.5, 0.5);
    CABasicAnimation *textAnimation = [CABasicAnimation animation];
    textAnimation.duration = 0.25;
    self.textLayer.actions = @{@"contents":textAnimation};
    [self addSublayer:self.textLayer];
    
    self.numberFormatter = [[NSNumberFormatter alloc] init];
    self.numberFormatter.numberStyle = NSNumberFormatterPercentStyle;
}

- (void)layoutSublayers{
    
    UIBezierPath *basePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    self.baseLayer.strokeColor = _baseTintColor ? _baseTintColor.CGColor : [UIColor darkGrayColor].CGColor;
    self.baseLayer.path = basePath.CGPath;
    
    self.progressLayer.strokeColor = _progressTintColor ? _progressTintColor.CGColor : [UIColor whiteColor].CGColor;
    
    self.textLayer.foregroundColor = _textColor ? _textColor.CGColor : [UIColor whiteColor].CGColor;

    UIFont *font = [UIFont fontWithName:[(UIFont *)self.textLayer.font fontName]
                                   size:self.textLayer.fontSize];
    CGFloat width = [self.textLayer.string widthForFont:font];
    CGFloat height = [self.textLayer.string heightForFont:font width:MAXFLOAT];
    self.textLayer.frame = CGRectMake(0, 0, width, height);
    self.textLayer.position = center;
    
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    
    center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0);
    radius = MIN(self.bounds.size.width, self.bounds.size.height) / 2.0;
}

- (void)setValue:(CGFloat)value{
    _value = value;
    
    UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius  startAngle:3 * M_PI_2 endAngle:3 * M_PI_2 + 2 * M_PI clockwise:YES];
    self.progressLayer.path = progressPath.CGPath;
    
    self.progressLayer.strokeEnd = value;
    
    CABasicAnimation *progressAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    progressAnimation.fromValue = nil;
    progressAnimation.toValue = @(value);
    progressAnimation.duration = 1.0;
    
    self.progressLayer.strokeEnd = value;
    [self.progressLayer addAnimation:progressAnimation forKey:@"progressAnimation"];
    
    self.textLayer.string = [self.numberFormatter stringFromNumber:@(value)];
    
}

- (void)setBaseTintColor:(UIColor *)baseTintColor{
    _baseTintColor = baseTintColor;
    
    CGColorRef baseColor = self.baseLayer.strokeColor;
    self.baseLayer.strokeColor = _baseTintColor ? _baseTintColor.CGColor : baseColor;
}

- (void)setProgressTintColor:(UIColor *)progressTintColor{
    _progressTintColor = progressTintColor;
    
    CGColorRef progressColor = self.progressLayer.strokeColor;
    self.progressLayer.strokeColor = _progressTintColor ? _progressTintColor.CGColor : progressColor;
    
}

- (void)setTextColor:(UIColor *)textColor{
    _textColor = textColor;
    
    CGColorRef oldTextColor = self.textLayer.foregroundColor;
    self.textLayer.foregroundColor = _textColor ? _textColor.CGColor : oldTextColor;
    
}

- (void)setFontSize:(CGFloat)fontSize {
    _fontSize = fontSize;
    CGFloat oldFontSize = self.textLayer.fontSize;
    self.textLayer.fontSize = _fontSize>0?_fontSize:oldFontSize;
}

- (void)setProgressSize:(CGFloat)progressSize {
    _progressSize = progressSize;
    [self setFrame:CGRectMake(0, 0, progressSize, progressSize)];
    if (self.superlayer) {
        self.position = (CGPoint){CGRectGetMidX(self.superlayer.bounds), CGRectGetMidY(self.superlayer.bounds)};
    }
    [self setValue:_value];
}

@end
